package com.ctsi.organise.app.rest;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.common.response.UnifiedResponse;
import com.ctsi.framework.elementui.support.TreeNode;
import com.ctsi.framework.organise.security.UserInfo;
import com.ctsi.framework.security.util.SecurityUtil;
import com.ctsi.organise.query.OrganiseQuery;
import com.ctsi.organise.service.IOrganiseService;
import com.ctsi.organise.service.OrganiseEntityManager;
import com.ctsi.organise.service.OrganiseStructManager;
import com.ctsi.organise.vo.AddOrganVO;
import com.ctsi.organise.vo.EditOrganVO;
import com.ctsi.organise.vo.OrganiseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: zhangjw
 * @Date: 2018/11/15 11:02
 * @Description:
 */
@RestController
public class OrganiseController {


    @Autowired
    private IOrganiseService organiseService;
    @Autowired
    private OrganiseEntityManager organiseEntityManager;

    @Autowired
    private OrganiseStructManager organiseStructManager;

    @Autowired
    private HttpServletRequest request;

    @PostMapping(value = "/organise/organises")
    public UnifiedResponse<Void> add (@RequestBody AddOrganVO organiseVO) {

        this.organiseService.addOrganise(organiseVO, SecurityUtil.getUser().getLoginId(),true);
        return UnifiedResponse.ok();
    }

    @GetMapping(value = "/organise/{partyId}")
    public UnifiedResponse<EditOrganVO> edit (@PathVariable("partyId") String partyId) {
        return UnifiedResponse.ok(this.organiseService.editPrivileged(SecurityUtil.getUser().getLoginId(), partyId));
    }

    @PutMapping(value = "/organise/update",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifiedResponse<Void> updata (@RequestBody EditOrganVO organVO) {
        UserInfo user = SecurityUtil.getUser();
        organiseService.updatePrivileged(user.getLoginId(), user.getUserId(), organVO);
        return UnifiedResponse.ok();
    }

    @DeleteMapping(value = "/organise/delete")
    public UnifiedResponse<Void> delete(@RequestParam("partyIds") String partyIds) {
        // 有子组织或者人员不允许删除
        if (UtilValidate.isNotEmpty(partyIds)) {
            String[] partyId = partyIds.split(",");
            for (String id : partyId) {
//               boolean candel = organiseEntityManager.isLeaf(id);
               this.organiseService.logicallyDelete(SecurityUtil.getUser().getLoginId(), id);
            }
        }
        return UnifiedResponse.ok();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping(value = "/organise/query",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public UnifiedResponse<Page<OrganiseVO>> query (@RequestBody OrganiseQuery query) {

        UserInfo user = SecurityUtil.getUser();
        return UnifiedResponse.ok(organiseService.page(query,user.getLoginId(), query.getStart(), query.getPageSize()));
    }

    @GetMapping(value = "/organise/organtree/{partyId}", produces = "application/json")
    @ResponseStatus(HttpStatus.OK)
    public UnifiedResponse<List<TreeNode>> organTree(@PathVariable("partyId") String partyId) {
        UserInfo info = SecurityUtil.getUser();
        return UnifiedResponse.ok(organiseService.getChildTreeNode(info.getLoginId(), partyId));
    }

    @GetMapping(value = "/organise/refresh")
    public UnifiedResponse<Void> refresh(){
        organiseStructManager.refreshStruct();
        return UnifiedResponse.ok();
    }

}
